import json
import time
import base64
import httpx

# 配置常量
HOST = 'http://127.0.0.1:5000'

# 传参全是64编码后的字符串
# 测试用户名
PLAYER_NAME = 'asdasd'
HEADERS = {'Content-Type': 'application/json'}
client = httpx.Client(timeout=1.0)


# 注册,但是只用交一个name,先简单搞
def register(name):
    try:
        response = client.put(
            f'{HOST}/register',
            params={'name': name}
        )
        return response.status_code == 200
    except httpx.RequestError as e:
        print(f"注册失败: {e}")
        return False


def find(name):
    try:
        response = client.get(
            f'{HOST}/find',
            params={'name': name}
        )
        return response.json()
    except httpx.RequestError as e:
        print(f"注册失败: {e}")
        return False


# 向服务端发送获取新游戏
def new_game(name):
    body = {
        'session_id': name,
        'time_limit': 114514
    }
    try:
        response = client.post(
            f'{HOST}/new_game',
            content=json.dumps(body),
            headers=HEADERS
        )
        # print(f"新游戏响应: {response.text}")
        return response.json()
    except httpx.RequestError as e:
        print(f"创建新游戏失败: {e}")
        return None


# 移动请求
def move_grid(name, direction):
    body = {
        'session_id': str(name),
        'direction': str(direction)
    }
    try:
        response = client.post(
            f'{HOST}/move',
            content=json.dumps(body),
            headers=HEADERS
        )
        # print(f"移动响应: {response.text}")
        return response.json()
    except httpx.RequestError as e:
        print(f"移动操作失败: {e}")
        return None


def win(name):
    body = {
        'session_id': str(name),
    }
    try:
        response = client.post(
            f'{HOST}/win',
            content=json.dumps(body),
            headers=HEADERS
        )
        # print(f"移动响应: {response.text}")
        return response.json()
    except httpx.RequestError as e:
        print(f"移动操作失败: {e}")
        return None


def lose(name):
    body = {
        'session_id': str(name),
    }
    try:
        response = client.post(
            f'{HOST}/lose',
            content=json.dumps(body),
            headers=HEADERS
        )
        return response.json()
    except httpx.RequestError as e:
        print(f"移动操作失败: {e}")
        return None


# 传参添加   name
def isGaming(name):
    try:
        response = client.get(
            f'{HOST}/isGaming',
            params={'name': name}
        )
        return response.json()
    except httpx.RequestError as e:
        print(f"查询游戏操作失败: {e}")
        return None


# 测试
def main():
    with httpx.Client(timeout=10.0) as client:
        # 预热连接
        # client.get(HOST)
        # HOST = 'http://localhost:5000'
        # PLAYER_NAME = 'asdasd'
        # HEADERS = {'Content-Type': 'application/json'}
        # client = httpx.Client(timeout=10.0)

        start_time = time.perf_counter()

        # 执行游戏操作
        new_game(PLAYER_NAME)
        move_grid(PLAYER_NAME, 'up')
        move_grid(PLAYER_NAME, 'up')
        move_grid(PLAYER_NAME, 'up')

        elapsed = time.perf_counter() - start_time
        print(f"请求总耗时: {elapsed:.3f} 秒")


if __name__ == '__main__':
    main()
